<template>
	<div class="set-act-box">
		<el-dialog
		  title="活动设置"
		  :visible.sync="dialogVisible"
		  width="30%"
		  :before-close="handleClose">
		  <div>
			  <span class="m-s-txt-lb">背景音乐:</span>
			  <el-radio v-model="radio" label="1" @change="stop()">默认</el-radio>
			  <el-radio v-model="radio" label="2" @change="stop()">关闭</el-radio>
			  <el-radio v-model="radio" label="3" @change="stop()">自定义</el-radio>
			  <span class="try-lisetion" v-if="radio != '2'" @click="tryMus">试听</span>
		  </div>
		  
		  <div class="o-u-box" v-if="radio == '3'">
				<span class="m-s-txt-lb">{{mp3Fn}}</span>
				<div class="u-btn" @click.stop="chooseFile">+上传替换</div> 
		  </div>
		  
		 <!-- <div class="o-u-box" v-if="radio == '3'">
				<span class="f-n-txt-lb">{{mp3Fn}}</span>
				<el-upload
					class="upload-demo"
					action=""
					multiple
					:limit="3">
					<el-button size="small" type="primary">+上传替换</el-button>
				</el-upload>
		  </div> -->
		</el-dialog>
	</div>
</template>

<script>
	import config from "@/js/conf/Config.js";
	import localStore from "@/js/LocalStore.js";
	import auPlayer from "@/js/util/audPlayer.js";
	export default
	{
		data()
		{
			return{
				dialogVisible:false,
				radio: '2',
				mp3Fn:"背景音乐 (mp3) :",
				act:"",
				music_url:"",
				hdongid:"",
				idyun:"",
				tryMusUrl:"",
				defMusUrl:"",
				musicUrlShort:"",
			}
		},
		
		methods:
		{
			stop()
			{
				auPlayer.stop();
			},
			tryMus()
			{
				if(this.radio == "1")//默认
				{
					this.tryMusUrl = this.defMusUrl;
				}
				else if(this.radio == "3")//自定义
				{
					if(!this.musicUrlShort)
					{
						this.$message({
						  message: '请上传背景音乐!',
						  type: 'warning'
						});
						return;
					}
					this.tryMusUrl = config.OSS_HOST + this.musicUrlShort;
				}
				console.log(this.tryMusUrl);
				auPlayer.creatAudioPlayer(this.tryMusUrl);
			},
			
			showSetWin(data)
			{
				this.dialogVisible = true;
				
				this.hdongid = this.$geturl().hdongid || "";
				this.idyun = this.$geturl().idyun || "";
				console.log(data)
				this.tryMusUrl = "";
				this.defMusUrl = "";
				this.musicUrlShort = "";
				this.act = data.act;
                console.log("音乐参数===：",data.data);
				if(data.data.ismusic == 2)//关闭
				{
					this.radio == "2";
				}
				else
				{
					if(data.data.music_url_short)//自定义
					{
						this.radio = "3";
						this.musicUrlShort = data.data.music_url_short;
					}
					else //默认
					{
						this.radio = "1";
					}
				}
				this.tryMusUrl = data.data.music_url;
				this.defMusUrl = data.data.music_url_default;
			},
			
			handleClose()
			{
				this.stop();
				let obj = {};
				obj.act = this.act;
				obj.data = {};
				if(this.radio == "1")//默认
				{
					obj.data.music_url = "";
					obj.data.ismusic = 1;
				}
				else if(this.radio == "2")//关闭
				{
					obj.data.music_url = "";
					obj.data.ismusic = 2;
				}
				else if(this.radio == "3")//自定义
				{
					// this.$msg("");
					if(!this.musicUrlShort)
					{
						this.$message({
						  message: '请上传背景音乐!',
						  type: 'warning'
						});
						return;
					}
					obj.data.ismusic = 1;
					obj.data.music_url = this.musicUrlShort;
				}
				this.dialogVisible = false;
				this.$emit('setBgMuisc',obj);
			},
			
			chooseFile()
			{
				let that = this;
				uni.chooseFile({
					count: 1,
					extension: [".mp3"],
					async success (res){
						if(!res)return;
						that.mp3Fn = res.tempFiles[0].name;
						let item = res.tempFilePaths[0];
						let url = config.COMM_ALI_OSS_SIGN;
						let param = {};
						param.act = "oss";
						let rData = await that.$api({ url: url, data: param });
						if(!rData)return;
						if(rData.status == 1)
						{
							if(!rData.data)return;
							rData = rData.data;
							let cp = await localStore.getCpInfo();
							let str = "d/cp" + cp.id + "/idyun" + that.idyun + "/hdongid"+ that.hdongid  + "/mp3/";
							String(item).split(".")[1];
							let fileName = str + new Date().getTime() + ".mp3";//+ String(item).split(".")[1];
							uni.uploadFile({
								url:rData.host,
								filePath:item,
								fileType:'audio',
								name:'file',
								formData:{
									name:fileName,
									key:fileName,
									policy:rData.policy,
									signature:rData.signature,
									OSSAccessKeyId: rData.accessid,
									success_action_status: '200', 
								},
								success: (result) => {
									that.musicUrlShort = fileName,
									that.$message({
									  message: '上传文件成功!',
									  type: 'success'
									});
								},
							});
						}
					},
					fail(err) {
						console.log(JSON.stringify(err))
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	.set-act-box
	{
		::v-deep.el-dialog
		{
			margin-top: 40vh !important;
		}
	}
	
	.m-s-txt-lb
	{
		padding-right: 10px;
	}
	
	.try-lisetion
	{
		color: #3388FF;
		cursor: pointer;
		position: absolute;
		right: 20px;
	}
	
	.o-u-box
	{
		display:flex;
		align-items: center;
		padding-top: 20px;
		.u-btn
		{
			margin-left: 10px;
			padding: 4px 20px;
			background-color: #3388FF;
			color:#FFF;
			border-radius: 4px;
			cursor: pointer;
		}
	}
	
</style>